package day52.pet.controller;

import day52.pet.service.IPetDetailService;
import day52.pet.service.IPetService;
import day52.pet.domain.Pet;
import day52.pet.query.PetQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    @Autowired
    private IPetDetailService petDetailService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        pet.setState(0);
        pet.setCreatetime(new Date());
        if( pet.getId()!=null){
            petService.update(pet);
        }else{
            petService.add(pet);
        }
        return AjaxResult.me();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        petService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id) {
        return petService.findOne(id);
    }


    /**
    * 查看所有的信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.findAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query) {

        return petService.pageQuery(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping("/dels")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        petService.patchDelete(ids);
        return AjaxResult.me();
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        petService.onsale(ids);
        return AjaxResult.me();
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        petService.offsale(ids);
        return AjaxResult.me();
    }

    /**
     * 查询上架信息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/onsaleList")
    public PageList<Pet> onsalelist(@RequestBody PetQuery query) {
        query.setState(1);
        return petService.pageQuery(query);
    }
}
